//
//  CXCycleView.m
//  TestCSV
//
//  Created by ChengxuZheng on 15/10/19.
//  Copyright © 2015年 ChengxuZheng. All rights reserved.
//

#import "CXCycleView.h"

@implementation CXCycleView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}


- (void)createAdScrollView {
    _adScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kHomePageHeaderHeight)];
    _adScrollView.pagingEnabled = YES;
    _adScrollView.showsHorizontalScrollIndicator = NO;
    _adScrollView.delegate = self;
    [self addSubview:_adScrollView];
}

- (void)createPageControl {
//    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kHomePageHeaderHeight-30, kSCREEN_WIDTH, 30)];
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = _adDataArray.count;
    _pageControl.currentPage = 0;
    _pageControl.enabled = NO;
    [self addSubview:_pageControl];
    
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(30);
    }];
}

- (void)setAdDataArray:(NSMutableArray *)adDataArray {
    _adIterval = 2.f;
    _adDataArray = [NSMutableArray array];
    _adDataArray = adDataArray;
    
    [self createAdScrollView];
    [self createPageControl];
    if (_adDataArray.count!=0) {
        [self loadDataThenShowAdvert];
    }
}

- (void)loadDataThenShowAdvert {
    NSInteger imageCount = _adDataArray.count;
    [_adScrollView setContentSize:CGSizeMake(kSCREEN_WIDTH*(imageCount+2), kHomePageHeaderHeight)];
    _pageControl.numberOfPages = imageCount;
    for (int i = 0; i < imageCount; i++) {
        UIImageView *adImageView = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*kSCREEN_WIDTH, 0, kSCREEN_WIDTH, kHomePageHeaderHeight)];
        adImageView.userInteractionEnabled = YES;
        
        
        [adImageView sd_setImageWithURL:[NSURL URLWithString:_adDataArray[i]]];
        [adImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adImageViewAction)]];
        [_adScrollView addSubview:adImageView];
    }
    
    UIImageView *lastAdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kHomePageHeaderHeight)];
    [lastAdImageView sd_setImageWithURL:[NSURL URLWithString:_adDataArray[imageCount-1]]];
    [_adScrollView addSubview:lastAdImageView];
    
    UIImageView *firstAdImageView = [[UIImageView alloc] initWithFrame:CGRectMake((imageCount+1)*kSCREEN_WIDTH, 0, kSCREEN_WIDTH, kHomePageHeaderHeight)];
    [firstAdImageView sd_setImageWithURL:[NSURL URLWithString:_adDataArray[0]]];
    [_adScrollView addSubview:firstAdImageView];
    [_adScrollView setContentOffset:CGPointMake(_adScrollView.bounds.size.width, 0)];
    

//    if (!_adTimer) {
//        _adTimer = [NSTimer scheduledTimerWithTimeInterval:_adIterval
//                                                    target:self
//                                                  selector:@selector(startShowCycleAdvert)
//                                                  userInfo:nil
//                                                   repeats:YES];
//    }
}

- (void)startShowCycleAdvert {
    
    CGFloat pageWidth = _adScrollView.frame.size.width;
    NSInteger currentPage = _adScrollView.contentOffset.x/pageWidth;
    if (currentPage == 0) {
        self.pageControl.currentPage = _pageControl.numberOfPages-1;
    }
    else if (currentPage == _pageControl.numberOfPages+1) {
        _pageControl.currentPage = 0;
    }
    else {
        _pageControl.currentPage = currentPage-1;
    }
    
    __block NSInteger currPageNumber = _pageControl.currentPage;
    CGRect rect = CGRectMake((currPageNumber+2)*pageWidth, 0, kSCREEN_WIDTH, kHomePageHeaderHeight);
    
    [UIView animateWithDuration:0.5 animations:^{
        [_adScrollView scrollRectToVisible:rect animated:NO];
    } completion:^(BOOL finished) {
        currPageNumber++;
        if (currPageNumber == _pageControl.numberOfPages) {
            [_adScrollView setContentOffset:CGPointMake(_adScrollView.bounds.size.width, 0)];
            currPageNumber = 0;
        }
    }];
    
    currentPage = _adScrollView.contentOffset.x/pageWidth;
    if (currentPage == 0) {
        _pageControl.currentPage = _pageControl.numberOfPages-1;
    }
    else if (currentPage == _pageControl.numberOfPages+1) {
        _pageControl.currentPage = 0;
    }
    else {
        _pageControl.currentPage = currentPage-1;
    }
   
}

#pragma mark - ScrollView delegate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    if (_adTimer) {
//        [_adTimer invalidate];
//        _adTimer = nil;
//    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentAdPage;
    currentAdPage = _adScrollView.contentOffset.x/kSCREEN_WIDTH;
    if (currentAdPage == 0) {
        [scrollView scrollRectToVisible:CGRectMake(kSCREEN_WIDTH*_pageControl.numberOfPages, 0, kSCREEN_WIDTH, kHomePageHeaderHeight) animated:NO];
        currentAdPage = _pageControl.numberOfPages-1;
    }
    else if (currentAdPage == (_pageControl.numberOfPages+1)) {
        [scrollView scrollRectToVisible:CGRectMake(kSCREEN_WIDTH, 0, kSCREEN_WIDTH, kHomePageHeaderHeight) animated:NO];
        currentAdPage = 0;
    }
    else{
        currentAdPage = currentAdPage-1;
    }
    self.pageControl.currentPage = currentAdPage;
    
//    if (!_adTimer) {
//        _adTimer = [NSTimer scheduledTimerWithTimeInterval:_adIterval
//                                                    target:self
//                                                  selector:@selector(startShowCycleAdvert)
//                                                  userInfo:nil
//                                                   repeats:YES];
//    }
}

#pragma mark - 用户事件
- (void)adImageViewAction {
    
    NSLog(@"%ld", _pageControl.currentPage);

}

@end
